//
//  STMLoginViewController.m
//  StoreManagement
//
//  Created by centling on 16/5/14.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMLoginViewController.h"
#import "STMGetCaptchaViewController.h"
#import "DLRadioButton.h"

@interface STMLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *IDTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwdButton;


@end

@implementation STMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MAINGRAYCOLOR;
    [self customNavigationTitle:@"登录"];
    
    [self.loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.forgetPwdButton addTarget:self action:@selector(forgetPwd) forControlEvents:UIControlEventTouchUpInside];
//    [SVProgressHUD showWithStatus:@"加载中..."];
}



- (void)login {
    if ([self.IDTextField.text isEqualToString:@""]) {
        [GlobalUtil HUDShowMessage:@"请输入账号" addedToView:self.view];
        return;
    }
    if ([self.passwordTextField.text isEqualToString:@""]) {
        [GlobalUtil HUDShowMessage:@"请输入密码" addedToView:self.view];
        return;
    }
    [STMNetworkAPI loginWithUserName:self.IDTextField.text
                            password:self.passwordTextField.text
                             success:^(STMLoginModel *result) {
                                 if ([result.code isEqualToString:@"200"]) {
                                     [UserDefaultHandler saveUserDetault:result.result.key key:kUserKey];
                                     [UserDefaultHandler saveUserDetault:result.result.seller_name key:kSellerName];
                                     [UserDefaultHandler saveUserDetault:result.result.member_name key:kMemberName];
                                     [UserDefaultHandler saveUserDetault:self.passwordTextField.text key:kUserPwd];
                                     [[NSNotificationCenter defaultCenter] postNotificationName:nHomePageRefresh object:nil];
                                     [self dismissViewControllerAnimated:YES completion:nil];
                                     if (_loginVCBlock) {
                                         _loginVCBlock();
                                     }
                                 } else {
                                     [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                 }
                             }
                             failure:^(NSError *err) {
                                 [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                             }];
}


- (void)forgetPwd {
    STMGetCaptchaViewController *vc = [[STMGetCaptchaViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
//    [self dismissViewControllerAnimated:NO completion:nil];
//    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    [nav pushViewController:vc animated:YES];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


- (void)back {
    UIViewController *pvc = self.presentingViewController;
    UINavigationController *nav = (UINavigationController *)pvc;
    [nav popViewControllerAnimated:NO];
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
